<?php
function https_request($url, $data = null)
{
    //dump($data);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    //var_dump(curl_error($curl));
    //dump($output);
    curl_close($curl);
    return $output;
}
function print_msg($content)
{
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    if (!empty($postStr)){
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
        if(!empty($keyword))
        {
            $msgType = "text";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
            echo $resultStr;
        }
    }else{
        echo "";
        exit;
    }
}
function transmitText($object, $content, $funcFlag = 0)
{
    $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
    $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag);
    return $resultStr;
}
function transmitImage($object, $media_id, $funcFlag = 0)
{
    $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
<FuncFlag>%d</FuncFlag>
</xml>";
    $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $media_id, $funcFlag);
    return $resultStr;
}
function transmitVoice($object, $media_id, $funcFlag = 0)
{
    $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>
<FuncFlag>%d</FuncFlag>
</xml>";
    $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $media_id, $funcFlag);
    return $resultStr;
}
function transmitVideo($object, $contenter, $funcFlag = 0)
{
    $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[%s]]></MediaId>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video>
<FuncFlag>%d</FuncFlag>
</xml>";
    $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $contenter['mediaid'], $contenter['title'], $contenter['description'], $funcFlag);
    return $resultStr;
}
function transmitMusic($object, $contenter, $funcFlag = 0)
{
    $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
</Music>
<FuncFlag>%d</FuncFlag>
</xml>";
    $arr=C("TMPL_PARSE_STRING");
    $contenter['musicurl']=$arr['__PUBLIC__'].$contenter['musicurl'];
    $contenter['hqmusicurl'] =$arr['__PUBLIC__'].$contenter['hqmusicurl'];
    $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(),$contenter['title'], $contenter['description'], $contenter['musicurl'], $contenter['hqmusicurl'], $contenter['thumbbediaid'], $funcFlag);
    return $resultStr;
}
function transmitNews($object, $arr_item, $funcFlag = 0)
{
    //首条标题28字，其他标题39字
    if(!is_array($arr_item))
        return;

    $itemTpl = "    <item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
    </item>
";
    $item_str = "";
    foreach ($arr_item as $item)
        $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

    $newsTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <Content><![CDATA[]]></Content>
    <ArticleCount>%s</ArticleCount>
    <Articles>
    $item_str</Articles>
    <FuncFlag>%s</FuncFlag>
    </xml>";

    $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $funcFlag);
    return $resultStr;
}
?>